package net.yb.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 通知/增强/切面
 */
@Component
@Aspect // 表示这是一个切面
public class LoginAdvice {

    // 定义切入点，指定切点表达式
    @Pointcut("execution(* net.yb.service.impl.*ServiceImpl.*(..))")
    public void pc(){
    }

    /**
     * 前置通知
     */
    @Before("pc()")
    public void before(JoinPoint joinPoint){ // 连接点
        System.out.println("在方法执行前记录日志........" + joinPoint.getSignature().getName() + "，参数：" + Arrays.toString(joinPoint.getArgs()));
    }

    /**
     * 后置通知
     */
    @AfterReturning(value = "pc()", returning = "returnValue")
    public void after(JoinPoint joinPoint, Object returnValue){
        System.out.println("在方法执行后记录日志........返回值：" + returnValue);
    }

    /**
     * 异常通知
     */
    @AfterThrowing(value = "pc()", throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Exception e){
        System.out.println("在方法出现异常时执行........异常信息：" + e.getMessage());
    }

}
